let that;
class Tab {
    constructor(id) {
        that = this;
        this.main = document.querySelector(id);
        this.add = this.main.querySelector('.tabadd');
        this.ul = this.main.querySelector('.firstnav ul:first-child');
        this.fsection = this.main.querySelector('.tabscon');
        this.init();
    }
    init() {
        // 初始化元素让相关的元素绑定事件
        this.updateNode()
        this.add.addEventListener('click', this.addTab)
        for (let i = 0; i < this.lis.length; i++) {
            this.lis[i].index = i;
            this.lis[i].addEventListener('click', this.toggleTab);
            this.remove[i].addEventListener('click', this.removeTab);
            this.spans[i].addEventListener('dblclick', this.editTab);
            this.sections[i].addEventListener('dblclick', this.editTab);
        }
    }
    // 动态添加元素，需要重新获取对应的元素
    updateNode() {
        this.lis = this.main.querySelectorAll('li');
        this.sections = this.main.querySelectorAll('section');
        this.remove = this.main.querySelectorAll('.icon-guanbi');
        this.spans = this.main.querySelectorAll('.firstnav li span:first-child');
    }
    // 1.切换功能
    toggleTab() {
        that.clearClass();
        this.setAttribute('class', 'liactive');
        that.sections[this.index].setAttribute('class', 'conactive');
    }
    clearClass() {
        for (let i = 0; i < this.lis.length; i++) {
            this.lis[i].setAttribute('class', '');
            that.sections[i].setAttribute('class', '');
        }
    }
    // 2.添加功能
    addTab() {
        that.clearClass();
        // 创建li元素和section元素
        const li = '<li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi"></span></li>';
        const section = '<section class="conactive">测试1</section>';
        // 把创建的li元素和section元素追加到相应的父元素里面
        that.ul.insertAdjacentHTML('beforeend', li);
        that.fsection.insertAdjacentHTML('beforeend', section);
        that.init();
    }

    // 3.删除功能

    removeTab(e) {
        e.stopPropagation();// 阻止冒泡
        // 根据索引号删除对应的li和section
        let index = this.parentNode.index;
        that.lis[index].remove();
        that.sections[index].remove();
        that.init();
        // 当我们删除的不是选定状态的li的时候，原来的选定状态的li保持不变
        if (document.querySelector('.liactive')) return;
        // 当我们删除了选定状态的这个li的时候，让它的前一个li处于选定状态
        index--;
        // 手动调用点击事件，不需要鼠标触发
        that.lis[index] && that.lis[index].click();
    }
    // 4.修改功能
    editTab() {
        const str = this.innerHTML;
        // 双击禁止选中文字
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        this.innerHTML = '<input type="text" />';
        const input = this.children[0];
        input.value = str;
        input.select();// 文本框里面的文字处于选定状态
        // 当离开文本框就把文本框里面的值给span
        input.addEventListener('blur', function () {
            this.parentNode.innerHTML = this.value;
        })
        // 按下回车也可以把文本框里面的值给span
        input.addEventListener('keyup', function (e) {
            if (e.keyCode === 13) {
                // this.parentNode.innerHTML = this.value;
                this.onblur();
            }
        })
    }
}
new Tab('#tab');